﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Mimoza.GUI;

namespace Mimoza.PuttyPlugin
{
    public partial class GeneralPage : Mimoza.GUI.WizardPage
    {
        private PuttyData m_data;
        public GeneralPage()
        {
            InitializeComponent();
        }

        override public void Init()
        {
            m_data = this.Wizard.UserData as PuttyData;

            textBoxSessionName.Text = m_data.SessionName;
            checkBoxInStartup.Checked = m_data.IsStartOnStartup;
            
            textBoxComputer.Text = m_data.Computer;
            textBoxPort.Text = m_data.Port.ToString();
            comboBoxType.SelectedIndex = (int)m_data.Type;
            textBoxUserName.Text = m_data.UserName;

            this.IsConfigured = CheckComputerName(false) && CheckSessionNameAndPort(false);

            this.comboBoxType.SelectedIndexChanged += new System.EventHandler(this.comboBoxType_SelectedIndexChanged);
            this.textBoxUserName.TextChanged += new System.EventHandler(this.textBoxUserName_TextChanged);
            this.textBoxPort.TextChanged += new System.EventHandler(this.textBoxPort_TextChanged);
            this.textBoxComputer.TextChanged += new System.EventHandler(this.textBoxComputer_TextChanged);
            this.checkBoxInStartup.CheckedChanged += new System.EventHandler(this.checkBoxInStartup_CheckedChanged);
            this.textBoxSessionName.TextChanged += new System.EventHandler(this.textBoxSessionName_TextChanged);
        }

        override public bool Try2LeavePage(WizardPageLeavingDirection direction)
        {
            if (direction == WizardPageLeavingDirection.Back)
            {
                return true;
            }

            if (!CheckSessionNameAndPort(true)) return false;
            if (!CheckComputerName(true)) return false;

            this.IsConfigured = true;
            m_data.SessionName = textBoxSessionName.Text;
            m_data.IsStartOnStartup = checkBoxInStartup.Checked;
            
            m_data.Computer = textBoxComputer.Text;
            m_data.Port = int.Parse(textBoxPort.Text);
            m_data.Type = (Type)comboBoxType.SelectedIndex;
            m_data.UserName = textBoxUserName.Text;

            return true;
        }

        bool CheckSessionNameAndPort(bool showMsg)
        {
            if (string.IsNullOrEmpty(textBoxSessionName.Text))
            {
                if (showMsg)
                {
                    MessageBox.Show(Resource.EmptySessionMessage, Resource.WrongParametersCaption,
                        MessageBoxButtons.OK, MessageBoxIcon.Error);
                    textBoxSessionName.Focus();
                }
                return false;
            }
            int port = 0;
            if (string.IsNullOrEmpty(textBoxPort.Text) || !int.TryParse(textBoxPort.Text,out port) || port<0)
            {
                MessageBox.Show(Resource.WrongPortMessage, Resource.WrongParametersCaption,
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                textBoxPort.Focus();
                return false;
            }
            return true;
        }

        bool CheckComputerName(bool showMsg)
        {
            if (string.IsNullOrEmpty(textBoxComputer.Text))
            {
                if (showMsg)
                {
                    MessageBox.Show(Resource.EmptyComputerNameMessage, Resource.WrongParametersCaption,
                        MessageBoxButtons.OK, MessageBoxIcon.Error);
                    textBoxComputer.Focus();
                }
                return false;
            }
            return true;
        }

        private void textBoxComputer_TextChanged(object sender, EventArgs e)
        {
            if (this.textBoxComputer.Text != null && this.textBoxComputer.Text.Length > 0)
            {
                this.IsConfigured = true;
            }
            this.RaiseOnPageModified();
        }

        private void comboBoxType_SelectedIndexChanged(object sender, EventArgs e)
        {
            textBoxUserName.Enabled = ((Type)comboBoxType.SelectedIndex == Type.SSH);
            this.RaiseOnPageModified();
        }

        private void textBoxSessionName_TextChanged(object sender, EventArgs e)
        {
            this.RaiseOnPageModified();
        }

        private void checkBoxInStartup_CheckedChanged(object sender, EventArgs e)
        {
            this.RaiseOnPageModified();
        }

        private void textBoxUserName_TextChanged(object sender, EventArgs e)
        {
            this.RaiseOnPageModified();
        }

        private void textBoxPort_TextChanged(object sender, EventArgs e)
        {
            this.RaiseOnPageModified();
        }
    }
}
